﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IMS_Data
{
    public class ProductReposity
    {
        public IList<product> GetProducts()
        {
            IList<product> productList = null;
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                productList = ctx.product.ToList();
            }
            return productList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="productName">empty for all</param>
        /// <returns></returns>
        public IList<product> GetProducts(string companyName,string productName)
        {
            IList<product> productList = null;
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                var sql = ctx.product.AsQueryable();
                if (!string.IsNullOrEmpty(companyName))
                {
                    sql = sql.Where(o=>o.company.company_name == companyName);
                }
                if (!string.IsNullOrEmpty(productName))
                {
                    sql = sql.Where(o=>o.product_name == productName);
                }
                productList = sql.ToList();
            }
            return productList;
        }

        public IList<product> GetProducts(int company_id)
        {
            IList<product> productList = null;
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                productList = (from o in ctx.product where o.company_id == company_id select o).ToList();
            }
            return productList;
        }

        public bool ExistProduct(product pro)
        {
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                var ele = (from o in ctx.product where o.product_sn == pro.product_sn select o).FirstOrDefault();
                if (ele == null)
                {
                    return true;
                }
            }
            return false;
        }

        public void AddProduct(product pro)
        {
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                ctx.product.AddObject(pro);
                ctx.SaveChanges();
            }
        }

        public void DeleteProduct(product pro)
        {
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                ctx.AttachTo(ctx.DefaultContainerName + ".product", pro);
                ctx.product.DeleteObject(pro);
                ctx.SaveChanges();
            }
        }
        public void UpdateProduct(product pro)
        {
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {

                ctx.product.Attach(pro);
                
                var entryState = ctx.ObjectStateManager.GetObjectStateEntry(pro);
                entryState.SetModifiedProperty("product_name");
                entryState.SetModifiedProperty("product_type");
                entryState.SetModifiedProperty("product_price");
                entryState.SetModifiedProperty("remarks");
                ctx.SaveChanges();
            }
        }
        public IList<product> QueryProduct(string sql)
        {
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                var eles = ctx.ExecuteStoreQuery<product>(sql);

                return eles.ToList<product>();
            }
        }

        public product GetProductByName(string name)
        {
            product productList = null;
            using (IMS_Data.IMS_DBEntities ctx = new IMS_Data.IMS_DBEntities())
            {
                productList = (from o in ctx.product where o.product_name == name select o).FirstOrDefault();
            }
            return productList;
        }
    }
}
